package be.ipl.ieco.servlets;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.DispatcherType;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@WebFilter(filterName = "Filtre", urlPatterns = "/*", dispatcherTypes = { DispatcherType.REQUEST })
public class Filtre implements Filter {
	@SuppressWarnings("unused")
	private FilterConfig fConfig;

	public void init(FilterConfig fConfig) throws ServletException {
		this.fConfig = fConfig;
	}

	public void destroy() {
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		if (!(request instanceof HttpServletRequest)) {
			String message = "Ce Site n'est accessible qu'en HTTP";
			request.setAttribute("message", message);
			request.getServletContext().getNamedDispatcher("ErreurParam")
					.forward(request, response);
			return;
		}
		HttpServletRequest req = (HttpServletRequest) request;
		if (!req.isRequestedSessionIdFromCookie()) {
			String message = "Vous devez accepter les cookies.";
			request.setAttribute("message", message);
			request.getRequestDispatcher("/index.html").forward(request,
					response);
			return;
		}
		
		ArrayList<String> authorizedPages = new ArrayList<String>();
		authorizedPages.add("/css/bootstrap-theme.css");
		authorizedPages.add("/css/bootstrap-theme.css.map");
		authorizedPages.add("/css/bootstrap-theme.min.css");
		authorizedPages.add("/css/bootstrap.css");
		authorizedPages.add("/css/bootstrap.css.map");
		authorizedPages.add("/css/bootstrap.min.css");
		authorizedPages.add("/css/bootstrap-datetimepicker.css");
		authorizedPages.add("/css/bootstrap-table.css");
		authorizedPages.add("/css/custom.css");
		authorizedPages.add("/css/custom_abdel.css");
		authorizedPages.add("/fonts/glyphicons-halflings-regular.eot");
		authorizedPages.add("/fonts/glyphicons-halflings-regular.svg");
		authorizedPages.add("/fonts/glyphicons-halflings-regular.ttf");
		authorizedPages.add("/fonts/glyphicons-halflings-regular.woff");
		authorizedPages.add("/images/logo.png");
		authorizedPages.add("/js/bootstrap.js");
		authorizedPages.add("/js/bootstrap.min.js");
		authorizedPages.add("/js/index.js");
		authorizedPages.add("/js/jquery-ui.js");
		authorizedPages.add("/js/jquery.js");
		authorizedPages.add("/js/bootstrap-datetimepicker.js");
		authorizedPages.add("/js/bootstrap-table.js");
		authorizedPages.add("/js/dropdown.js");
		authorizedPages.add("/js/mindmup-editabletable.js");
		authorizedPages.add("/js/numeric-input-example-crudEtudiant.js");
		authorizedPages.add("/js/numeric-input-example-crudOption.js");
		authorizedPages.add("/js/numeric-input-example-pro-eco.js");
		authorizedPages.add("/js/prettify.js");
		authorizedPages.add("/js/npm.js");
		authorizedPages.add("/api.html");
		authorizedPages.add("/index.html");

		HttpSession session = req.getSession();
		synchronized (session) {
			Integer level = (Integer) session.getAttribute("level");
			if (level != null) { // connecte
				if(level == 0){
					authorizedPages.add("/crudEtudiant.html");
					authorizedPages.add("/crudOption.html");
					authorizedPages.add("/delibe.html");
					authorizedPages.add("/intersectionChoixOptions.html");
					authorizedPages.add("/intersectionsOptions.html");
					authorizedPages.add("/parametres.html");
					authorizedPages.add("/proEco.html");
					authorizedPages.add("/serie.html");
					authorizedPages.add("/visualisationEtudiantParOption.html");
					authorizedPages.add("/visualiserOptions.html");
				} else if(level>0){
					authorizedPages.add("/choix.html");
				}
			}
		}

		
		String nom = req.getServletPath();
		if (!authorizedPages.contains(nom)) {
			String message = "La resource que vous demandez n'existe pas ou n'est pas accessible";
			request.setAttribute("message", message);
			request.getServletContext().getNamedDispatcher("ErreurParam")
					.forward(request, response);
			return;

		}

		chain.doFilter(request, response);
	}

}
